package com.apes.fn.scm.retailOrder.service.tc.vipSurplus.report;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.ReflectUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Title: VipSurplusDailyService
 * @Description: 会员联盟日报表
 * @author: ZhengZeXian   expenses 支出,income 收入
 * @create: 2022/1/14 9:22
 */
@Service("vipSurplusDailyService")
public class VipSurplusDailyService extends DomainService {

    @Autowired
    private VipSurplusDailyRepository vipSurplusDailyRepository;
    @Autowired
    private SequenceService sequenceService;

    public void reportCollect(SimpleRequest request){
        Date date = DateUtil.dateToDate(DateUtil.add(new Date(), Calendar.DATE, -1), DateUtil.SHOW_DATE_FORMAT);
//        JSONObject param = request.getJO();
//        if (param.getDate("collectDate") !=null){
//            date = param.getDate("collectDate");
//        }
        List<VipSurplusDaily> vipSurplusDailies = vipSurplusDailyRepository.findByDate(date);
        PartyRole  platformCompany = SpringManager.getBean(PartyRoleRepository.class).findOne("1007");

        vipSurplusDailies.stream().forEach(data -> {
            VipSurplusDaily vipSurplusDailyExpenses = ReflectUtil.clone(data, "id");
            VipSurplusDaily vipSurplusDailyIncome = ReflectUtil.clone(data, "id");

            vipSurplusDailyExpenses.setId(sequenceService.getId(VipSurplusDaily.CONST_TABLE_NAME));
            vipSurplusDailyExpenses.setType("expenses");
            vipSurplusDailyExpenses.setPlatformCompany(platformCompany);
            //vipSurplusDailieDatas.add(vipSurplusDailyExpenses);
            vipSurplusDailyRepository.saveAndFlush(vipSurplusDailyExpenses);


            vipSurplusDailyIncome.setId(sequenceService.getId(VipSurplusDaily.CONST_TABLE_NAME));
            vipSurplusDailyIncome.setType("income");
            vipSurplusDailyIncome.setCompany(vipSurplusDailyIncome.getBelongCompany());
            vipSurplusDailyIncome.setPlatformCompany(platformCompany);
            vipSurplusDailyRepository.saveAndFlush(vipSurplusDailyIncome);

       });
       //vipSurplusDailyRepository.saveAll(vipSurplusDailieDatas);
       //vipSurplusDailyRepository.flush();
    }

}
